package hou.river;

/**
 * @author houweitao
 * 2015年7月2日 下午3:57:25
 */


import hou.river.dao.RiverDao;
import hou.river.dao.impl.RiverDaoImpl;
import hou.river.model.DepthData;
import hou.river.util.ConnectionFactory;
import hou.river.util.Matrix;

import java.sql.Connection;
import java.sql.SQLException;

public class Test2 {
	static double astar1 = 0;
	static double astar2 = 0;
	static double depth[] = new double[6];

	public static void main(String[] args) throws SQLException {
		Connection conn = ConnectionFactory.getInstance().makeConnection();
		RiverDao rd = new RiverDaoImpl();

		double yn[] = new double[11];
		DepthData dd = new DepthData();

//		dd.setX(597113);
//		dd.setY(3365853);
//		depth = rd.getDepth(conn, dd);
//		for (int i = 0; i < 12; i++)
//			System.out.println(depth[i]);
		
		depth[0] = 2.67;
		depth[1] = 3.13;
		depth[2] = 3.25;
		depth[3] = 3.36;
		depth[4] = 3.56;
		depth[5] = 3.72;

		double[] cgs = buildCumulativeGeneratingSequence(depth);
		System.out.println("累加序列：");
		for (int i = 0; i < 5; i++)
			System.out.println(cgs[i]);
		
		double[][] b = bulidB(cgs);

		for (int i = 0; i < 5; i++){
			yn[i] = depth[i + 1];
			System.out.println("yn"+i+"="+yn[i]);
		}

		// Matrix matrix = new Matrix(4, 4);
		// matrix.New();
		Matrix matrix = new Matrix(b);
		System.out.println("矩阵B");
		matrix.Output();

		Matrix tran = matrix.Trans();
		System.out.println("矩阵B的转置");
		tran.Output();

		Matrix result = tran.Multiply(matrix).Invs().Multiply(tran);
		System.out.println("矩阵B的转置 乘 矩阵B，的逆矩阵，再乘以矩阵B的转置");
		result.Output();

		for (int i = 0; i < 5; i++) {
			astar1 = astar1 + result.mat[0][i] * yn[i];
			astar2 = astar2 + result.mat[1][i] * yn[i];
		}

		System.out.println("satar1 & astar2");
		System.out.println(astar1);
		System.out.println(astar2);

		// depth[0];

		int k = 8;
		outDeep(k);
	}

	private static void outDeep(int k) {
		double r =  (depth[0] - (astar2 / astar1))*Math.pow(Math.E, astar1 * -k)
				+ astar2 / astar1;
		double r2 = (depth[0] - (astar2 / astar1))*Math.pow(Math.E, astar1 * -(k-1))
				+ astar2 / astar1;

		System.out.println(r);
		System.out.println(r2);
		System.out.println(r - r2);
	}

	private static double[][] bulidB(double[] cgs) {
		double[][] tmp = new double[5][2];
		for (int i = 0; i < 5; i++) {
			tmp[i][0] = (cgs[i] + cgs[i + 1]) * (-0.5);
			tmp[i][1] = 1;
		}

		return tmp;
	}

	private static double[] buildCumulativeGeneratingSequence(double[] depth) {
		double[] cgs = new double[6];
		for (int i = 0; i < 6; i++)
			cgs[i] = 0;

		for (int i = 0; i < 6; i++) {
			for (int j = 0; j <= i; j++)
				cgs[i] = cgs[i] + depth[j];
		}

		return cgs;
	}
}


